<template>
  <div class="evaluate-container">
    <div class="evaluate-header">
      <h2 class="header-title">所有评价</h2>
      <el-button class="return-button" @click="goBack">返回</el-button>
    </div>
    <div class="evaluate-list">
      <el-table :data="pagedEvaluates" style="width: 100%">
        <el-table-column prop="member_name" label="用户名"></el-table-column>
        <el-table-column prop="evaluate_date" label="评价日期" :formatter="formatDate"></el-table-column>
        <el-table-column prop="content" label="评价内容"></el-table-column>
        <el-table-column prop="rating" label="评价等级"></el-table-column> <!-- 新增列 -->
        <el-table-column label="操作" v-if="userIdentity === 'mem'">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              v-if="scope.row.member_name === currentUserName"
              @click="deleteEvaluate(scope.row.evaluate_id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      evaluates: [],
      userIdentity: '', // 用户身份
      currentUserName: '', // 当前登录的用户名
      currentPage: 1,
      pageSize: 10,
      total: 0,
    };
  },
  computed: {
    pagedEvaluates() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.evaluates.slice(start, end);
    },
  },
  created() {
    this.fetchEvaluates();
    this.getUserIdentity();
    this.getCurrentUserName();
  },
  methods: {
    async fetchEvaluates() {
      try {
        const response = await axios.get('/api/evaluate/all');
        if (response.status === 200) {
          this.evaluates = response.data;
          this.total = response.data.length;
        } else {
          this.$message.error('获取评价失败');
        }
      } catch (error) {
        console.error('获取评价时发生错误:', error);
        this.$message.error('获取评价时发生错误');
      }
    },
    goBack() {
      this.$router.push('/member');
    },
    async deleteEvaluate(evaluate_id) {
      try {
        const response = await axios.delete(`/api/evaluate/delete/${evaluate_id}`);
        if (response.status === 200) {
          this.$message.success('删除成功');
          this.fetchEvaluates(); // 重新获取评价列表
        } else {
          this.$message.error('删除失败');
        }
      } catch (error) {
        console.error('删除评价时发生错误:', error);
        this.$message.error('删除评价时发生错误');
      }
    },
    getUserIdentity() {
      // 假设从 localStorage 获取用户身份
      this.userIdentity = localStorage.getItem('userIdentity');
    },
    getCurrentUserName() {
      // 假设从 localStorage 获取当前登录的用户名
      this.currentUserName = localStorage.getItem('currentUserName');
    },
    formatDate(row, column, cellValue, index) {
      if (cellValue) {
        return cellValue.split('T')[0]; // 假设后端返回的日期格式为 ISO 格式
      }
      return '';
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
    },
    handleCurrentChange(newPage) {
      this.currentPage = newPage;
    },
  },
};
</script>

<style scoped>
.evaluate-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  background-image: url('@/assets/img/evaluate.jpg');
  background-size: cover;
  background-position: center;
  height: 100vh;
  overflow-y: auto;
}

.evaluate-header {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  max-width: 800px;
  margin-bottom: 20px;
  position: relative;
}

.header-title {
  text-align: center;
  margin: 0 auto;
}

.return-button {
  position: absolute;
  top: 0;
  right: 0;
}

.evaluate-list {
  width: 100%;
  max-width: 800px;
}
</style>